/* Copyright (c) 2008, 2009, 2010, 2011 Damian Kmiecik
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

#ifndef DHCPC_H_
#define DHCPC_H_

#include "main.h"

#define DHCPC_DEBUG	DEBUG

#ifdef USE_DHCPC

struct DHCP_HEADER {
	uint8_t op;
	uint8_t htype;
	uint8_t hlen;
	uint8_t hops;
	uint8_t xid[4];
	uint16_t secs;
	uint16_t flags;
	uint8_t ciaddr[4];
	uint8_t yiaddr[4];
	uint8_t siaddr[4];
	uint8_t giaddr[4];
	uint8_t chaddr[16];
	uint8_t sname[64];
	uint8_t file[128];
	uint8_t options[312];
};

struct DHCPC_CACHE {
	uint8_t type;
	uint8_t ovld;
	uint8_t router_ip[4];
	uint8_t dns1_ip[4];
	uint8_t netmask[4];
	uint8_t lease[4];
	uint8_t serv_id[4];
};

#define DHCPDISCOVER	1
#define DHCPOFFER		2
#define DHCPREQUEST		3
#define DHCPDECLINE		4
#define DHCPACK			5
#define DHCPNAK			6
#define DHCPRELEASE		7
#define DHCPINFORM		8

#define DHCP_STATE_IDLE				0
#define DHCP_STATE_DISCOVER_SENT	1
#define DHCP_STATE_OFFER_RCVD		2
#define DHCP_STATE_SEND_REQUEST		3
#define DHCP_STATE_REQUEST_SENT		4
#define DHCP_STATE_ACK_RCVD			5
#define DHCP_STATE_NAK_RCVD			6
#define DHCP_STATE_ERR				7
#define DHCP_STATE_FINISHED			8

#define DHCP_CLIENT_PORT	HTONS(68)
#define DHCP_SERVER_PORT	HTONS(67)

extern uint8_t dhcpc_state;
extern uint32_t dhcpc_lease;
extern struct DHCPC_CACHE dhcpc_cache;

extern void dhcpcInit(void);
extern void dhcpcPacket(uint8_t);
extern void dhcpcGet(void);
extern uint8_t dhcpc(void);
extern void dhcpcParseOptions(uint8_t*, struct DHCPC_CACHE*, uint16_t);

#endif

#endif /* DHCPC_H_ */
